<?php

/**
 * 
 * 打折优惠前台控制器
 *  
 */
class DazheAction extends HomeAction {

    public function _initialize() {
        parent::_initialize();
        $this->seo('打折优惠 ');
    }

    function index() {
        //地区列表
        $areas = M('Area')->where(array('status' => 1))->select();
        //分类
        $cat     = D('Cat')
                ->field("*,cat_id as id,parent_id as parentid")
                ->where(array('parent_id' => 0))
                ->order(array("sort_order" => "ASC"))
                ->select();
        //商家选项
        $shop        = M('Shop')->select();
        $aid         = format_int($_REQUEST['aid']);
        $cid         = format_int($_REQUEST['cid']);
        $keyword     = t($_REQUEST['search']);

        //查询打折信息
        $dazhemod = M('Dazhe');
        $order    = ' create_time DESC ';
        $limit    = 10;

        if ($aid) {
            $map['d.area_id'] = $aid;
        }
        //分类置顶|首页置顶
        if ($cid) {
            $map['d.cat_id'] = $cid;
            $order .= ',cat_top_status DESC ';
        } else {
            $order .= ',index_top_status DESC ';
        }

        if ($keyword) {
            $map['d.title'] = array('LIKE', '%' . $keyword . '%');
        }
        //取得满足条件的记录数
        $count = $dazhemod
                ->join('AS d JOIN ' . C('DB_PREFIX') . 'cat as c ON c.cat_id=d.cat_id ')
                ->where($map)
                ->count($dazhemod->getPk());

        if ($count > (intval($_REQUEST['page']) - 1) * $limit) {
            import("ORG.Util.Page");
            //创建分页对象
            $p = new Page($count, $limit);
            //分页查询数据

            $voList = $dazhemod
                    ->join('AS d JOIN ' . C('DB_PREFIX') . 'cat as c ON c.cat_id=d.cat_id ')
                    ->where($map)
                    ->order($order)
                    ->field('d.*, c.name as cat_name')
                    ->limit($p->firstRow . ',' . $p->listRows)
                    ->select();
            //分页显示
            $page   = $p->show();
            //模板赋值显示
            $data   = array(
                'list'      => $voList,
                'page'      => $page,
                'nowPage'   => $_REQUEST['page'],
                'totalPage' => round($count / $limit),
            );
        } else {
            $data = array(
                'list'      => 0,
                'page'      => '',
                'nowPage'   => $_REQUEST['page'],
                'totalPage' => round($count / $limit),
            );
        }

        if (IS_AJAX) {
            $this->ajaxReturn($data);
        } else {
            //推荐信息
            $roundDZ = _roundDazhe();
            $this->assign('rounddz', $roundDZ);
            $this->assign('data', $data);
            $this->assign('shop', $shop);
            $this->assign('area', $areas);
            $this->assign('cat', $cat);
            $this->display();
        }
    }

    public function show() {
        $id       = $this->_get('id');
        $dazhemod = M('Dazhe');
        $map      = array('d.dazhe_id' => $id);
        $dazhe       = $dazhemod
                ->join('AS d JOIN ' . C('DB_PREFIX') . 'cat as c ON c.cat_id=d.cat_id ')
                ->where($map)
                ->field('d.*, c.name as cat_name')
                ->find();
        $dazhemod->where(array('dazhe_id' => $id))->setInc('view_count');
        //发布天数限制
        if ($dazhe['show_end_time'] < time()) {
            $this->error('已过期');
        }
        //@todo 评论
        $commit = M('Comment');
        $map    = array(
            'con_id'   => $id,
            'con_type' => 'dazhe',
        );
        $count     = $commit
                ->where($map)
                ->count($commit->getPk());

        if ($count > 0) {
            import("ORG.Util.Page");
            //创建分页对象
            $p = new Page($count, 10);
            //分页查询数据

            $voList     = $commit
                    ->where($map)
                    ->order('create_time DESC')
                    ->limit($p->firstRow . ',' . $p->listRows)
                    ->select();
            //分页显示
            $page       = $p->show();
            //模板赋值显示
            $commitlist = array(
                'list'      => $voList,
                'page'      => $page,
                'nowPage'   => $_REQUEST['page'],
                'totalPage' => round($count / $limit),
            );
        }

        //推荐信息
        $roundDZ = _roundDazhe();
        $this->assign('rounddz', $roundDZ);

        $this->assign('comment', $commitlist);
        $this->assign('dz', $dazhe);
        $this->display();
    }

}
